<template>
    <div class="e-form">
        <BillTop :title="$route.meta.title" @cancel="handleClose"></BillTop>
        <div class="tabs warningTabs"></div>
        <div class="tabs" v-if="Object.keys(formData).length != 0">
            <el-tabs tab-position="left" v-model="tabsName" @tab-click="onChangeTab">
                <el-tab-pane
                    label="基本信息"
                    name="baseInfo"
                    :disabled="clickTabFlag"
                ></el-tab-pane>
                <el-tab-pane label="附件信息" name="attach" :disabled="clickTabFlag"></el-tab-pane>
                <el-tab-pane
                    label="审核历史"
                    name="auditHistory"
                    :disabled="clickTabFlag"
                ></el-tab-pane>
                <div>
                    <div id="baseInfoCon" class="con">
                        <div class="tabs-title" id="baseInfo">基本信息</div>
                        <div style="width: 100%" class="form">
                            <el-form ref="form" :model="form" label-width="170px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="采购编号" width="220">
                                            <div style="display: flex">
                                                <span>{{ formData.tenderApplyNo }}</span>
                                                <!-- <el-input v-model="formData.tenderApplyNo" disabled></el-input> -->
                                                <el-button
                                                    style="margin-left: 10px"
                                                    class="btn-greenYellow"
                                                    size="mini"
                                                    @click="showBidding"
                                                    >查看</el-button
                                                >
                                            </div>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="采购单位">
                                            <span>{{ formData.tenderUser }}</span>
                                            <!-- <el-input v-model="formData.tenderUser"></el-input> -->
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="招标类型">
                                            <span>{{
                                                formData.tenderType | tenderClass(this)
                                            }}</span>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="关闭日期">
                                            <span>{{ formData.gmtCreate }}</span>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="采购方式">
                                            <span>{{
                                                formData.tenderForm | tenderForm(this)
                                            }}</span>
                                            <!-- <el-select v-model="formData.tenderForm" placeholder="请选择" @change="selectChange($event, '采购方式')">
                                                <el-option
                                                    v-for="(item, index) in options.tenderForm"
                                                    :key="index"
                                                    :label="item.kvKey"
                                                    :value="item.kvId"
                                                >
                                                </el-option>
                                            </el-select> -->
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <!-- 传id、名称closeOrgId -->
                                        <el-form-item label="关闭机构">
                                            <span>{{ formData.closeOrgName }}</span>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="拟采购项目名称" width="200">
                                            <span>{{ formData.tenderName }}</span>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="拟采购金额（元）">
                                            <span>{{ formData.tenderAmount }}</span>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-form-item label="采购需求陈述/ 采购项目概况*：">
                                        <span>{{ formData.tenderDemand }}</span>
                                        <!-- <el-input type="textarea" :rows="2" v-model="formData.tenderDemand"> </el-input> -->
                                    </el-form-item>
                                </el-row>
                                <el-row>
                                    <el-form-item label="关闭原因：">
                                        <span>{{ formData.closeReason }}</span>
                                        <!-- <el-input type="textarea" :rows="2" v-model="formData.closeReason"> </el-input> -->
                                    </el-form-item>
                                </el-row>
                                <el-row>
                                    <el-col :span="24" style="margin-top: 10px">
                                        <el-form-item label="状态">
                                            <span>{{ formData.state | state(this) }}</span>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                        </div>
                    </div>

                    <div id="attachCon" class="con">
                        <div class="tabs-title" id="attach">附件信息</div>
                        <div class="e-table">
                            <div class="top">
                                <div class="left">
                                    <el-button size="mini">下载</el-button>
                                    <el-button size="mini">删除</el-button>
                                </div>
                            </div>
                            <el-table
                                :data="tableData"
                                border
                                style="width: 100%"
                                @cell-mouse-enter="cellMouseEnter"
                                @cell-mouse-leave="cellMouseLeave"
                                :cell-class-name="cellClsNm"
                            >
                                <el-table-column show-overflow-tooltip width="55">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="a1" label="序号">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="a" label="文件名称">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="b" label="文件大小">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="c" label="上传时间">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="d" label="上传人">
                                </el-table-column>
                            </el-table>
                            <ComPagination />
                            <div class="upload">
                                <el-upload
                                    class="upload-demo"
                                    drag
                                    action="https://jsonplaceholder.typicode.com/posts/"
                                    multiple
                                >
                                    <el-button type="primary" style="padding: 20px 80px"
                                        >上传附件</el-button
                                    >
                                    <div style="margin-top: 10px; font-size: 12px">
                                        鼠标拖入上传，按住Ctrl可多选附件
                                    </div>
                                </el-upload>
                            </div>
                        </div>
                    </div>

                    <div id="auditHistoryCon" class="con">
                        <div class="tabs-title" id="auditHistory">审核历史</div>
                        <div class="e-table">
                            <el-table
                                :data="tableData"
                                border
                                style="width: 100%"
                                @cell-mouse-enter="cellMouseEnter"
                                @cell-mouse-leave="cellMouseLeave"
                                :cell-class-name="cellClsNm"
                            >
                                <el-table-column show-overflow-tooltip prop="a1" label="审核级次">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="a" label="审核人">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="b" label="审核人岗位">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="c" label="通知时间">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="c" label="审核时间">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="c" label="审核意见">
                                </el-table-column>
                            </el-table>
                        </div>
                        <ComPagination />
                    </div>
                </div>
            </el-tabs>
        </div>
        <div class="buttons">
            <el-button @click="handleClose">取消</el-button>
        </div>
        <div id="seat" :style="{ height: seatHeight + 'px' }"></div>
    </div>
</template>

<script>
import '@/utils/jquery.scrollTo.min'
// import ComTreeTable from '@/templates/table/treeTable.vue'
// import ComTreeFile from '@/templates/table/treeFile.vue'
import ComPagination from '@/components/pagination/pagination.vue'
import applyCloseRequest from '@/api/tender/applyClose.js'
import applyRequest from '@/api/tender/apply.js'
import { mapState } from 'vuex'

export default {
    data () {
        return {
            tabsName: 'baseInfo',
            screenWidth: 0,
            screenHeight: 0,
            lastConHeight: 0,
            clickTabFlag: false, // 如果手动点击时，则不做滚动事件逻辑，只有滚动屏幕时才走滚动事件逻辑
            form: {
                name: '',
                region: '',
                date1: '',
                date2: '',
                delivery: false,
                type: [],
                resource: '',
                desc: ''
            },
            tableData: [],
            value: '',
            textarea: '',
            formData: {
                state: 0
            },
            test: '',
            selectData: [],
            selectTableColumnAttr: [
                {
                    label: '单据编号',
                    prop: 'billNo'
                },
                {
                    label: '拟招标项目名称',
                    prop: 'tenderName'
                },
                {
                    label: '单据日期',
                    prop: 'gmtCreate'
                },
                {
                    label: '单据机构名称',
                    prop: 'orgName'
                }
            ],
            selectTableColumnWidth: {
                leftWidth: '150',
                rightWidth: '100'
            },
            selectDataPagination: {}
        }
    },
    computed: {
        ...mapState({
            options: state => state.biddingApplication.dictionaries
        }),
        billId () {
            return this.$route.query.billId || this.$route.query.billid
        },
        // 填补底部空白，以使高度够滚动
        seatHeight () {
            return this.screenHeight - 72 - this.lastConHeight
        }
    },
    components: {
        // ComTreeTable,
        // ComTreeFile,
        ComPagination
    },
    created () {
        this.getData()
    },
    mounted () {
        // 保存所有tabName
        const arr = ['baseInfo', 'attach', 'auditHistory']
        // 获取最后一个内容区域的高度，计算底部空白
        this.getLastConHeight(arr)
        let $idsTop = []
        window.addEventListener('scroll', () => {
            // 如果手动点击时，则不做滚动事件逻辑，只有滚动屏幕时才走滚动事件逻辑
            if (this.clickTabFlag) {
                return
            }

            if (!$idsTop[$idsTop.length - 1]) {
                $idsTop = arr.map(item => {
                    const itemTop = document.getElementById(item)?.offsetTop
                    return itemTop
                })
            }
            const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
            // 倒序查找
            let curLocal = 0
            for (let len = $idsTop.length, i = len - 1; i >= 0; i--) {
                let item = $idsTop[i]
                if (scrollTop + 1 >= item) {
                    curLocal = i
                    break
                }
            }
            // 设置对应tabName
            this.tabsName = arr[curLocal]
        })

        this.screenWidth = document.documentElement.clientWidth - this.topHeight
        this.screenHeight = document.documentElement.clientHeight - this.topHeight
        window.addEventListener('resize', () => {
            this.screenWidth = document.documentElement.clientWidth - this.topHeight
            this.screenHeight = document.documentElement.clientHeight - this.topHeight
            $idsTop = arr.map(item => {
                const itemTop = document.getElementById(item)?.offsetTop
                return itemTop
            })
        })
    },
    beforeDestroy () {
        this.$notify.closeAll()
    },
    methods: {
        onChangeTab (e) {
            const height = $('#' + e.name).offset().top
            $.scrollTo(height - this.topHeight, 500)
            // 如果手动点击时，则不做滚动事件逻辑，只有滚动屏幕时才走滚动事件逻辑
            this.clickTabFlag = true
            // 动画结束后，恢复状态
            setTimeout(() => {
                this.clickTabFlag = false
            }, 600)
        },
        // 获取最后一个内容区域的高度，计算底部空白
        getLastConHeight (arr) {
            let si = setInterval(() => {
                // 因为dom异步加载，通过轮询找到渲染后的dom，获取高度
                if (document.getElementById(arr[length - 1] + 'Con')) {
                    const lastConHeight = document.getElementById(
                        arr[length - 1] + 'Con'
                    )?.offsetHeight
                    this.lastConHeight = lastConHeight
                    clearInterval(si)
                    si = null
                }
            }, 100)
        },
        getData () {
            applyCloseRequest.tenderApplyCloseFindById(this.billId).then(res => {
                this.formData = res
            })
        },
        showBidding () {
            if (!this.tenderId) return // 前期需求可切换，现在不需要
            this.$notify.closeAll()
            this.$notify({
                title: '查看招标详情',
                dangerouslyUseHTMLString: true,
                message: `正式环境：客户端打开新页面<br>
                测试环境：手动打开新页面<br>
                <textarea cols="35" rows="10">http://localhost:8080/biddingApplicationShow?tenderId=${this.tenderId}</textarea>
                `,
                duration: 0
            })
            // this.$router.push({
            //     path: './biddingApplicationShow',
            //     query: { tenderId: this.tenderId }
            // })
        },
        selectCurrentChange (page) {
            this.tenderApplyQueryPage({ limit: 20, page })
        },
        selectDataSearch (searchValue) {
            this.tenderApplyQueryPage({ key: searchValue, limit: 20, page: 1 })
        },
        selectDataConfirm (data) {
            applyRequest.tenderApplyFindById(data.billId).then(res => {
                this.state = 'main'
                this.formData = { ...this.formData, ...res }
            })
        },
        tenderApplyNoInputFocus () {
            this.$refs.tenderApplyNoInput.blur()
        },
        stateChange (name) {
            this.state = name
            if (name === 'selectData') {
                this.tenderApplyQueryPage()
            }
        },
        tenderApplyQueryPage (params = { limit: 20, page: 1 }) {
            applyRequest.tenderApplyQueryPage(params).then(res => {
                this.selectData = res.list
                this.selectDataPagination.total = res.totalCount
            })
        },
        getNowDate () {
            // 个位数前补0
            const zeroFill = function (num) {
                if (parseInt(num) < 10) {
                    num = '0' + num
                }
                return num
            }
            let nowtime = new Date()
            nowtime =
                zeroFill(nowtime.getFullYear()) +
                '-' +
                zeroFill(nowtime.getMonth() + 1) +
                '-' +
                zeroFill(nowtime.getDate())
            this.$set(this.formData, 'applyTime', nowtime)
        },
        handleFullScreen () {
            this.fullscreen = !this.fullscreen
        },
        handleClose () {
            this.$router.push({
                path: './equipHireBiddingApplicationEdit',
                query: { billid: this.tenderId, seat: 'close' }
            })
        },
        cellClsNm ({ column }) {
            if (column.label === '') {
                return 'fixed-left'
            }
        },
        cellMouseEnter (row) {
            this.MouseEnterId = row.id
        },
        cellMouseLeave () {
            this.MouseEnterId = 0
        }
    }
}
</script>

<style lang="scss" scoped>
.form {
    margin-top: 10px;
}

.e-table {
    background: #fff;

    /* .top {
    .left {
      padding: 20px;
      padding-top: 0px;
    }
  } */
}

.upload {
    margin: 20px auto;
    display: flex;
    justify-content: center;
    text-align: center;
}

.upload-demo {
    display: flex;
    justify-content: center;
    align-items: center;
}
</style>
